8642. Four digit Armstrong numbers

 

A four-digit number is called an Armstrong number if the sum of the fourth powers of its digits equals the number itself.

For example: 8208 = 84 + 24  + 04 + 84, therefore, 8208 is an Armstrong number.

Print all Armstrong numbers in the range from a to b.

 

Input. Two integers a and b (1000 ≤ ab ≤ 9999).

 

Output. Print all Armstrong numbers in a single line in the range from a to b.

 

Sample input

Sample output

1000 3000

1634

 

 

SOLUTION

loops

 

Algorithm analysis

Let’s iterate through the numbers from a to b. For each number i =  (aib) extract the digits of thousands x, hundreds y, tens z, and ones u. If the number i is an Armstrong number (i = x4 + y4 + z4 + u4), print it.

 

Algorithm implementation

Read the input data.

 

scanf("%d %d", &a, &b);

 

Iterate through the numbers from a to b.

 

for (i = a; i <= b; i++)

{

 

Extract the digits of the number i = .

 

  x = a / 1000;

  y = i / 100 % 10;

  z = i / 10 % 10;

  u = i % 10;

 

If the number i is an Armstrong number (i = x4 + y4 + z4 + u4), print it.

 

  if (x * x * x * x + y * y * y * y + z * z * z * z +

      u * u * u * u == i) printf("%d ", i);

}

 

printf("\n");

 

Python implementation

Read the input data.

 

a, b = map(int, input().split())

 

Iterate through the numbers from a to b.

 

for i in range(a, b + 1):

 

Extract the digits of the number i = .

 

  x = i // 1000

  y = (i // 100) % 10

  z = (i // 10) % 10

  u = i % 10

 

If the number i is an Armstrong number (i = x4 + y4 + z4 + u4), print it.

 

  if x**4 + y**4 + z**4 + u**4 == i:

    print(i, end=" ")

 

print()